package server;

import java.util.LinkedList;
import java.util.List;


/**
 * This class provides the functionality that C.R.U.D.s seeds.
 * It also provides search functionality for seeds.
 * @author Valerie
 */
public class SeedHandler {
    private static LinkedList<Seed> seedList = new LinkedList<Seed>();
    public SeedHandler(){}
    
    /**
     * Gets a specific seed based on the seed id
     * 
     * @param seedID The id of the seed that is being requested
     * @return The seed.
     */
    
    public Seed getSeed(int seedID){
    	TextInterface.loadSeeds();
        if(!seedList.isEmpty()){
                for(Seed s : seedList){
                    if(s.getSeedID()==seedID){
                        return s;
                    }
                }
            }
            return null;
    }
    
    /**
     * Gets all seeds.
     * 
     * @return A LinkedList of all seeds that currently exist.
     */
    
    public static LinkedList<Seed> getSeeds(){
    	return seedList;
    }
    
    /**
     * Sets all the seeds to a specific list. Used for importing the list of seeds from the
     * serialized pages.
     * 
     * @param s The linkedlist to set the seed list to.
     */
    public static void setSeeds(LinkedList<Seed> s){
    	seedList = s;
    }
    /**
     * Creates a seed.
     * 
     * @param comName The seed's common name.
     * @param sciName The seed's scientific name
     * @param hardiness The seed's hardiness zone number
     * @param reqs Requirements for growing the seed
     * @param tips Tips for growing the seed
     * @param harvestInfo Information on when the seed can be harvested
     * @param startGrow The start of the seed's harvest/growing period
     * @param stopGrow The end of the seed's harvest/growing period
     * @param farmer The farmer who owns the seed
     * @return Whether or not a seed has been created.
     */
    
    public boolean makeSeed(String comName, String sciName, int hardiness, String reqs,
			String tips, String harvestInfo, String startGrow, String stopGrow, String farmer){
        Seed s = new Seed(comName, sciName, hardiness, reqs, tips, harvestInfo, startGrow, stopGrow, farmer);
        seedList.add(s);
        TextInterface.saveSeeds();
        return true;
    }
    /**
     * Finds multiple seeds based on search parameters.
     * 
     * @param criteria The search criteria. One of four things.
     * @param param The search parameter.
     * @return A linkedlist of all of the seeds matching the search criteria
     */
    
    public static LinkedList<Seed> findSeed(String criteria, String param){
    	TextInterface.loadSeeds();
    	LinkedList<Seed> result = new LinkedList<Seed>();
        	for(Seed s:getSeeds()){
        		if(s.getFarmerID().equals(criteria) && param.equals("farmer")){
        			result.add(s);
                        } else if(s.getComName().toLowerCase().contains(criteria.toLowerCase()) && param.equals("cname")){
                            result.add(s);
                        } else if(s.getSciName().toLowerCase().contains(criteria.toLowerCase()) && param.equals("sname")){
                            result.add(s);
                        } else if(Integer.toString(s.getHardiness()).equals(criteria) && param.equals("hardiness")){
                            result.add(s);
                        }
        	}
        return result;
    }
    /**
     * NYI: updates a specific seed based on parameters.
     * @return Whether a seed was successfully updated.
     */
    public boolean updateSeed(){
        return false;
    }
    
    /**
     * NYI: Removes a seed.
     * @param seedID The ID of the seed to be removed
     * @return Whether a seed was successfully removed.
     */
    public boolean removeSeed(int seedID){
        return false;
    }
    
}
